package com.br4mmie.asciistory;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
/**
 * Animation class, Contains a series of CharImages played in sequence to create an Animation
 * @author Niels
 *
 */
public class Animation {

	private int numberOfFrames; //number of frames this animation consists of
	private int currentframe; //the current frame that will be displayed
	private int width; //the width of the images
	private int height; //the height of the images
	private int loops; //number of times this animation will be displayed
	private int locX, locY; //location of the image
	private int delay; //the delay between the frames


	public boolean isPlaying; //the animation is running
	ArrayList<CharImage> frames; //ArrayList containing the separate Images

	/**
	 * Constructor, Loads an Animation from file 
	 * @param filename  File to load
	 * @param x X coordinate 
	 * @param y Y Coordinate
	 */
	public Animation(String filename, int x, int y) 
	{
		locX = x;
		locY = y;
		currentframe = 0;
		loops = 0;
		frames = new ArrayList<CharImage>();
		BufferedReader in;
		try
		{
			in = new BufferedReader(new FileReader(FileGetter.getFilename(filename)));
			width = Integer.parseInt(in.readLine());
			height = Integer.parseInt(in.readLine());
			numberOfFrames = Integer.parseInt(in.readLine());
			delay = Integer.parseInt(in.readLine());
			for(int i = 0; i < numberOfFrames; i++)
			{
				in.readLine();
				CharImage image = new CharImage(width, height);
	            for(int j =0; j < height; j++)
	            {
	            	image.writeImage(in.readLine(),0,j);
	            }
	            image.setTransparent('.');
	            image.createMask();
	            frames.add(image);
			}
			
		}
		catch(FileNotFoundException e)
		{
			System.out.println("File not found " + filename);
		}
		catch(Exception e)
		{
			System.out.println("BLUH in  Animation");
		}
	}
	
	/**
	 * Stop the Animation
	 */
	public void stop()
	{
		isPlaying = false;
	}
	
	/**
	 * Resume the Animation
	 */
	public void resume()
	{
		isPlaying = true;
	}
	
	/**
	 * Start the animation
	 * @param i number of times to play the animation
	 */
	public void play(int i) {
		isPlaying = true;
		loops = i;
	}
	
	/**
	 * OnDraw function, draws the animation to the target
	 * @param target image to draw to
	 */
	public void onDraw(CharImage target){
		frames.get(currentframe).draw(target, locX, locY);
		
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//System.out.println("BLUH in animation");
	}
	
	/**
	 * OnLoop function, sets the next frame
	 */
	public void onLoop(){
		if(isPlaying){
			if(currentframe < numberOfFrames-1)currentframe++;
			else {
				currentframe = 0;
				loops--;
				if(loops <= 0)isPlaying = false;
			}
			
		}

	}
	
	public int getLocX() {
		return locX;
	}

	public void setLocX(int locX) {
		this.locX = locX;
	}

	public int getLocY() {
		return locY;
	}

	public void setLocY(int locY) {
		this.locY = locY;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

}
